<?php
namespace app\api\controller;

use app\api\model\SortModel;

 use think\Controller;
use think\Db;
use think\Request;
use think\Cache;
use think\Cookie;
use think\Session;
class Role extends Common
{


    public function index()
    {
        $db=Db::name("role");
        
        if($query) $w['role_name'] = array('like','%'.$query.'%');
        $w['id'] = array('neq',1);
        $list = Db::name('role')
        ->field('id,role_name,role_tags,role_auth_ids,status')
        ->where($w)
        ->select();
        foreach ($list as $k => $v) {
            // 一级栏目where
            $res = array();
            if($v['role_auth_ids']){
                $lm1_where['id'] = array('in',$v['role_auth_ids']);
                $lm1_where['pid'] = array('eq',0);
                $lm1_where['status'] = array('eq',1);
                $lm1 = Db::name('sort')->field('id,pid,catname')->where($lm1_where)->order('id asc')->select();
                foreach ($lm1 as $k1 => $v1) {
                    $res[$k1] = $lm1[$k1];
                    // 二级栏目where
                    $lm2_where['id'] = array('in',$v['role_auth_ids']);
                    $lm2_where['pid'] = array('eq',$v1['id']);
                    $lm2_where['status'] = array('eq',1);
                    // 二级栏目
                    $lm2 = Db::name('sort')->field('id,pid,catname')->where($lm2_where)->order('pid asc')->select();
                    // 三级栏目
                    foreach ($lm2 as $k2 => $v2) {
                        // 三级栏目where
                        $lm3_where['id'] = array('in',$v['role_auth_ids']);
                        $lm3_where['pid'] = array('eq',$v2['id']);
                        $lm3_where['status'] = array('eq',1);
                        $lm3 = Db::name('sort')->field('id,pid,catname')->where($lm3_where)->order('pid asc')->select();
                        $lm2[$k2]['children'] = $lm3;
                    }

                    $res[$k1]['children'] = $lm2;
                }
                $list[$k]['children'] = $res;
            }
            unset($list[$k]['role_auth_ids']);
            $list[$k]['status'] = $v['status']==1?true:false;
        }
       
        $arr['data']=$list;
        $arr['meta']=array('msg'=>'获取角色列表成功','status'=>200);
        return json_encode($arr);

    }

    public function add()
    {
        $data=input('post.');
        $data['status']=$data['status']?1:0;
        // dump($data);
        // die();
        $id=input('id',0);
        $db=Db::name("role");
        $v=$db->find($id);
        

        // print_r($data['role_auth_ids']);die;
        if(Request::instance()->isPost()){

            if($id){
                if(Db::name("role")->where("id=".$id)->update($data)){
                    $data['id'] = $id;
                    $arr['data']=$data;
                    $arr['meta']=array('msg'=>'修改角色成功','status'=>200);
                }else{
                    $arr['data']=null;
                    $arr['meta']=array('msg'=>'修改角色失败','status'=>300);
                }
            }else{
                $data['sendtime']=time();
                if($id = Db::name("role")->insert($data)){
                    $data['id'] = $id;
                    $arr['data']=$data;
                    $arr['meta']=array('msg'=>'添加角色成功','status'=>200);
                }else{
                    $arr['data']=null;
                    $arr['meta']=array('msg'=>'添加角色失败','status'=>300);
                }
            }

            return json_encode($arr);

        }
    }

    public function add2()
    {
        $data=input('post.');
        $data['status']=$data['status']?1:0;
        // dump($data);
        // die();
        $id=input('id',0);
        $db=Db::name("role");
        $v=$db->find($id);
        if($v){
            $v['role_auth_ids']=explode(",",$v['role_auth_ids']);
        }else{
            $v['role_name']="";
            $v['role_auth_ids']=array();
            $v['status']=1;
            $v['id']='';

        }

        // print_r($v['role_auth_ids']);die;
        if(Request::instance()->isPost()){

            $auth_ids=implode(",",$data['checkList']);//把数组分割成字符串
            if($auth_ids){
                // 综合勾选的一级栏目数据
                $pids=$this->role_auth_ids($auth_ids);
                $data['role_auth_ids']=$auth_ids.$pids;
                // 前端勾选的数据
                $data['role_auth_check']=$auth_ids;
                unset($data['checkList']);
            }

            // print_r($data);die;
            if($id){
                if(Db::name("role")->where("id=".$id)->update($data)){
                    $data['id'] = $id;
                    $arr['data']=$data;
                    $arr['meta']=array('msg'=>'修改角色成功','status'=>200);
                }else{
                    $arr['data']=null;
                    $arr['meta']=array('msg'=>'修改角色失败','status'=>300);
                }
            }else{
                $data['sendtime']=time();
                if($id = Db::name("role")->insert($data)){
                    $data['id'] = $id;
                    $arr['data']=$data;
                    $arr['meta']=array('msg'=>'添加角色成功','status'=>200);
                }else{
                    $arr['data']=null;
                    $arr['meta']=array('msg'=>'添加角色失败','status'=>300);
                }
            }

            return json_encode($arr);

        }



       // print_r($v);die;
        $this->assign("v",$v);
        return $this->fetch("/role/add");
    }

    public function del()
    {
        $id=input('id',0);

        if(Db::name("role")->delete($id)){
            $arr['data']=null;
            $arr['meta']=array('msg'=>'删除成功','status'=>200);
        }else{
            $arr['data']=null;
            $arr['meta']=array('msg'=>'删除失败','status'=>300);
        }

        return json_encode($arr);die;

    }

    // 测试递归
    public function get_sort_id($id=0){
        $list = Db::name("sort")->field('id')->where("pid in({$id})")->select();
        static $arr = array();
            if($list){
                foreach ($list as $k => $v) {
                    // return变量名
                    $arr[] = $v['id'];
                    // 递归参数
                    $arr2[] = $v['id'];
                }
                $pid = implode(',',$arr2);
                $this->get_sort_id($pid);
            }
        return $arr;
    }

    public function delete()
    {
        $roleid=input('roleid',0);
        $rightid=input('rightid',0);

        $list = Db::name("role")->field('role_auth_check,role_auth_ids')->where("id = {$roleid}")->find();
        // dump($this->get_sort_id($rightid));die();
        $xx = $this->get_sort_id($rightid);
        $xx[] = intval($rightid);
        // dump($xx);die();

        $arr = explode(',',$list['role_auth_ids']);
        foreach ($arr as $k => $v) {
            if(in_array($v,$xx)){
                unset($arr[$k]);
            }
        }
        $arr2 = explode(',',$list['role_auth_check']);
        foreach ($arr2 as $k => $v) {
            if(in_array($v,$xx)){
                unset($arr2[$k]);
            }
        }
        // dump($list['role_auth_ids']);

        $list['role_auth_ids'] = implode(',',$arr);
        $list['role_auth_check'] = implode(',',$arr2);

        // dump($list['role_auth_ids']);die();
        if(Db::name('role')->where("id = {$roleid}")->update($list)){
            $w['id'] = array('eq',$roleid);
            $list = Db::name('role')
            ->field('id,role_name,role_tags,role_auth_ids,status')
            ->where($w)
            ->select();
            foreach ($list as $k => $v) {
                // 一级栏目where
                if($v['role_auth_ids']){
                    $lm1_where['id'] = array('in',$v['role_auth_ids']);
                    $lm1_where['pid'] = array('eq',0);
                    $lm1_where['status'] = array('eq',1);
                    $lm1 = Db::name('sort')->field('id,pid,catname')->where($lm1_where)->order('id asc')->select();
                    foreach ($lm1 as $k1 => $v1) {
                        $res[$k1] = $lm1[$k1];
                        // 二级栏目where
                        $lm2_where['id'] = array('in',$v['role_auth_ids']);
                        $lm2_where['pid'] = array('eq',$v1['id']);
                        $lm2_where['status'] = array('eq',1);
                        // 二级栏目
                        $lm2 = Db::name('sort')->field('id,pid,catname')->where($lm2_where)->order('pid asc')->select();
                        // 三级栏目
                        foreach ($lm2 as $k2 => $v2) {
                            // 三级栏目where
                            $lm3_where['id'] = array('in',$v['role_auth_ids']);
                            $lm3_where['pid'] = array('eq',$v2['id']);
                            $lm3_where['status'] = array('eq',1);

                            $lm3 = Db::name('sort')->field('id,pid,catname')->where($lm3_where)->order('pid asc')->select();
                            $lm2[$k2]['children'] = $lm3;
                        }

                        $res[$k1]['children'] = $lm2;
                    }
                    $list[$k]['children'] = $res;
                }
                unset($list[$k]['role_auth_ids']);
                $list[$k]['status'] = $v['status']==1?true:false;
            }
           
            $arr['data']=$list[0]['children'];
            $arr['meta']=array('msg'=>'删除成功','status'=>200);
        }else{
            $arr['data']=null;
            $arr['meta']=array('msg'=>'删除失败','status'=>300);
        }

        return json_encode($arr);die;

    }

    public function audit()
    {

        $id=input("id",0);
        //echo $id;
        $v=Db::name("role")->find($id);
        $data['status']=$v['status']?0:1;

        if(Db::name("role")->where("id=".$id)->update($data)){
            $arr['data']=$v;
            $arr['meta']=array('msg'=>'获取角色状态成功','status'=>200);
        }else{
            $arr['data']=$v;
            $arr['meta']=array('msg'=>'获取角色状态失败','status'=>300);
        }

        echo json_encode($arr);
    }
}